package com.lanbu.system.service.impl;

import java.util.Date;
import java.util.List;

import com.lanbu.common.annotation.DataScope;
import com.lanbu.common.core.domain.entity.SysDept;
import com.lanbu.common.core.domain.entity.SysUser;
import com.lanbu.common.exception.ServiceException;
import com.lanbu.common.utils.DateUtils;

import com.lanbu.common.utils.SecurityUtils;
import com.lanbu.common.utils.StringUtils;
import com.lanbu.common.utils.bean.BeanUtils;
import com.lanbu.common.utils.spring.SpringUtils;
import com.lanbu.system.domain.SysReviewUser;
import com.lanbu.system.mapper.SysReviewUserMapper;
import com.lanbu.system.mapper.SysUserMapper;
import com.lanbu.system.service.ISysDeptService;
import com.lanbu.system.service.ISysReviewUserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 用户信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-23
 */
@Service
public class SysReviewUserServiceImpl implements ISysReviewUserService {
    @Autowired
    private SysReviewUserMapper sysReviewUserMapper;

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private ISysDeptService deptService;

    /**
     * 查询用户信息
     *
     * @param applicantId 用户信息主键
     * @return 用户信息
     */
    @Override
    public SysReviewUser selectSysReviewUserByApplicantId(Long applicantId) {
        return sysReviewUserMapper.selectSysReviewUserByApplicantId(applicantId);
    }

    /**
     * 查询用户信息列表
     *
     * @param sysReviewUser 用户信息
     * @return 用户信息
     */
    @Override
    @DataScope(deptAlias = "r", userAlias = "r")
    public List<SysReviewUser> selectSysReviewUserList(SysReviewUser sysReviewUser) {


        return sysReviewUserMapper.selectSysReviewUserList(sysReviewUser);
    }

    /**
     * 新增用户信息
     *
     * @param sysReviewUser 用户信息
     * @return 结果
     */
    @Override
    public int insertSysReviewUser(SysReviewUser sysReviewUser) {
        sysReviewUser.setCreateTime(DateUtils.getNowDate());
        return sysReviewUserMapper.insertSysReviewUser(sysReviewUser);
    }

    /**
     * 修改用户信息
     *
     * @param sysReviewUser 用户信息
     * @return 结果
     */
    @Override
    public int updateSysReviewUser(SysReviewUser sysReviewUser) {
        sysReviewUser.setUpdateTime(DateUtils.getNowDate());
        return sysReviewUserMapper.updateSysReviewUser(sysReviewUser);
    }

    /**
     * 批量删除用户信息
     *
     * @param applicantIds 需要删除的用户信息主键
     * @return 结果
     */
    @Override
    public int deleteSysReviewUserByApplicantIds(Long[] applicantIds) {
        return sysReviewUserMapper.deleteSysReviewUserByApplicantIds(applicantIds);
    }

    /**
     * 删除用户信息信息
     *
     * @param applicantId 用户信息主键
     * @return 结果
     */
    @Override
    public int deleteSysReviewUserByApplicantId(Long applicantId) {
        return sysReviewUserMapper.deleteSysReviewUserByApplicantId(applicantId);
    }

    /**
     * 审核用户信息
     *
     * @param sysReviewUser
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int examineSysReviewUser(SysReviewUser sysReviewUser) {

        SysUser user = SecurityUtils.getLoginUser().getUser();

        if (sysReviewUser == null || sysReviewUser.getDeptId() == null) {
            throw new ServiceException("传递参数为空！");
        }
        //判断是否为驳回，2为驳回，1为未审核
        if ("2".equals(sysReviewUser.getExamine())) {
            //驳回审核
            SysReviewUser reviewUser = sysReviewUserMapper.selectSysReviewUserByApplicantId(sysReviewUser.getApplicantId());
            reviewUser.setExamine("2");
            reviewUser.setReviewerId(user.getUserId());
            reviewUser.setReviewer(user.getUserName());
            user.setUserId(null);
            reviewUser.setUpdateTime(new Date());
            sysReviewUserMapper.updateSysReviewUser(reviewUser);
            return 1;
        } else if ("1".equals(sysReviewUser.getExamine())) {
            return 1;
        }

        //通过审核
        //1，通过id查出审核表中数据
        SysReviewUser reviewUser = sysReviewUserMapper.selectSysReviewUserByApplicantId(sysReviewUser.getApplicantId());
        if (reviewUser == null) {
            throw new ServiceException("审核信息为空！");
        }
        //2，添加审核人id和审核人名称
        reviewUser.setReviewerId(user.getUserId());
        reviewUser.setReviewer(user.getUserName());
        user.setUserId(null);
        SysUser sysUser = sysUserMapper.selectUserByUserName(reviewUser.getUserName());
        if (sysUser != null) {
            throw new ServiceException("该工号已存在！");
        }
        SysUser Userphonenumber = sysUserMapper.findUserByPhoneNumber(reviewUser.getPhonenumber());
        if (Userphonenumber != null) {
            throw new ServiceException("该手机号已存在！");
        }
        //3，对象拷贝，审核表拷贝到用户表
        SysUser saveUser = new SysUser();
        BeanUtils.copyBeanProp(saveUser, reviewUser);
        //4,密码加密,设置创建人
        saveUser.setPassword(SecurityUtils.encryptPassword(reviewUser.getPassword()));
        saveUser.setCreateBy(user.getCreateBy());
        saveUser.setDeptId(sysReviewUser.getDeptId());
        //5，添加用户
        int result = sysUserMapper.insertUser(saveUser);
        reviewUser.setExamine(sysReviewUser.getExamine());
        reviewUser.setUserId(saveUser.getUserId());
        reviewUser.setUpdateBy(user.getUserName());
        reviewUser.setUpdateTime(new Date());
        //6，更新审核表信息
        int updatedSysReviewUser = sysReviewUserMapper.updateSysReviewUser(reviewUser);
        return updatedSysReviewUser;
    }
}
